package com.example.ice.fittime.fragment;


import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.ViewPropertyAnimation;
import com.example.ice.fittime.MainActivity;
import com.example.ice.fittime.R;
import com.example.ice.fittime.adapter.SocialRecyclerAdapter;
import com.example.ice.fittime.entry.Banners;
import com.example.ice.fittime.entry.UserActivity;
import com.example.ice.fittime.global.ClassType;
import com.example.ice.fittime.util.NetUtil;
import retrofit2.Call;
import retrofit2.Callback;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Objects;


import retrofit2.Response;
import retrofit2.Retrofit;

import static android.R.id.accessibilityActionScrollToPosition;
import static android.R.id.message;
import static android.R.id.text1;

/**
 * A simple {@link Fragment} subclass.
 */
public class SocialFragment extends Fragment implements Callback<List>, View.OnClickListener, SwipeRefreshLayout.OnRefreshListener, Toolbar.OnMenuItemClickListener {
   

    private RecyclerView recyclerView;
    private String TAG = this.getClass().getName();
    private SocialRecyclerAdapter adapter;
    private List<Object> objects =null;
    private Object object = null;
    private TextView header_name;
    private TextView header_update;
    private ImageView header_pic;
    private TextView header_name1;
    private TextView header_update1;
    private ImageView header_pic1;
    private int pager ;
    private boolean isloading ;
    private SwipeRefreshLayout swipe;

    public SocialFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        loadUADataMore(pager,"hot",30);
        NetUtil.getData(ClassType.Banners,this);
        return inflater.inflate(R.layout.fragment_social, container, false);
    }

    @Override
    public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
        recyclerView = (RecyclerView) view.findViewById(R.id.social_recycleview);
        swipe = (SwipeRefreshLayout) view.findViewById(R.id.social_swipe);
        swipe.setOnRefreshListener(this);
        Toolbar social_toolbar = (Toolbar) view.findViewById(R.id.social_toolbar);
        social_toolbar.setNavigationIcon(R.mipmap.ic_message_home);
        social_toolbar.setTitle("关注");
        social_toolbar.setNavigationOnClickListener(this);
        social_toolbar.setOnMenuItemClickListener(this);
        ((MainActivity) getActivity()).setSupportActionBar(social_toolbar);

        final AppBarLayout social_appbar = (AppBarLayout) view.findViewById(R.id.social_appbar);
        final RelativeLayout coll1 = (RelativeLayout) view.findViewById(R.id.social_coll1);
        final RelativeLayout coll2 = (RelativeLayout) view.findViewById(R.id.social_coll2);


         header_name =(TextView) coll1.findViewById(R.id.social_recycler_item2_header_name);
         header_update = (TextView) coll1.findViewById(R.id.social_recycler_item2_header_update);
         header_pic = (ImageView) coll1.findViewById(R.id.social_recycler_item2_header_pic);

        header_name1 =(TextView) coll2.findViewById(R.id.social_recycler_item2_header_name);
        header_update1 = (TextView) coll2.findViewById(R.id.social_recycler_item2_header_update);
        header_pic1 = (ImageView) coll2.findViewById(R.id.social_recycler_item2_header_pic);
        adapter = new SocialRecyclerAdapter(new ArrayList<>(), getContext(),this);
        recyclerView.setAdapter(adapter);
        final TextView name1 = (TextView) coll1.findViewById(R.id.social_recycler_item2_header_name);

        itemDecoration();
        scrollListener(social_appbar, coll1, coll2);
    }

    private void itemDecoration() {
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDraw(c, parent, state);
                c.drawColor(Color.GRAY);
            }
            @Override
            public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDrawOver(c, parent, state);
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.set(0,0,0,2);
            }
        });
    }

    private void scrollListener(final AppBarLayout social_appbar, final RelativeLayout coll1, final RelativeLayout coll2) {
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

                super.onScrolled(recyclerView, dx, dy);
                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                int last = layoutManager.findLastVisibleItemPosition();
                int first = layoutManager.findFirstVisibleItemPosition();
                if(first>=adapter.getItemCount()-5 && !isloading ){
                    loadUADataMore(++pager,"hot",30);
                }
                    if (first >= 2) {
                        if (first == 2) {
                            coll1.setVisibility(View.VISIBLE);
                        }
                        View firstView = layoutManager.findViewByPosition(first);
                        View lastView = layoutManager.findViewByPosition(last);
                        int Ltop = lastView.getTop();
                        int fTop = firstView.getTop();

                        UserActivity userActivity = adapter.getObjectItem(first);
                        if (userActivity != null) {
                            header_name.setText(userActivity.getUserDetail().getName());
                            header_update.setText("22小时前");
                            adapter.loadCircularImageView(header_pic, userActivity.getUserDetail().getAvatar_url());
                        }
                        if (first >= 3 && Ltop <= coll1.getMeasuredHeight()) {
                            coll2.setVisibility(View.VISIBLE);
                        }
                        if (Ltop >= 0 && Ltop <= coll1.getMeasuredHeight()) {
                            UserActivity userActivity1 = adapter.getObjectItem(last);
                            if (userActivity != null) {
                                header_name1.setText(userActivity1.getUserDetail().getName());
                                header_update1.setText("22小时前");
                                adapter.loadCircularImageView(header_pic1, userActivity1.getUserDetail().getAvatar_url());
                            }
                            int detalY = 0;
                            detalY = Ltop - coll1.getMeasuredHeight();
                            ViewCompat.setTranslationY(coll1, detalY);
                            ViewCompat.setTranslationY(coll2, detalY);
                            social_appbar.setVisibility(View.INVISIBLE);
                        }

                        if (dy <0) {
                            social_appbar.setVisibility(View.INVISIBLE);
                            int top1 = lastView.getTop();
                            if (top1 >=0) {
                                coll2.setVisibility(View.GONE);
                            }
                        }
                    } else {
                        coll1.setVisibility(View.GONE);
                    }
            }
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

        });
    }

    public void loadUADataMore(int pager ,String type ,int limit ){
        long time = new Date().getTime();
        NetUtil.getData(ClassType.UserActivity,this,type,Long.toBinaryString(time),limit+"",""+pager);
        isloading =!isloading;
    }


    @Override
    public void onResponse(Call<List> call, Response<List> response) {
        int firstSize = 0;
        List body = response.body();
        if (body != null) {
            if (body.get(0) instanceof Banners) {
                object = body;
                adapter.add(0,object);
                adapter.add(1, new String("click"));
            }
            if (body.get(0) instanceof UserActivity) {
                objects =body;
            }
            if (objects!=null && adapter.getItemCount()!=0){
                adapter.addAll(objects);
                isloading = false;
                if(swipe.isRefreshing()) {
                swipe.setRefreshing(false); };
            }
        }
    }
    @Override
    public void onFailure(Call<List> call, Throwable t) {

    }



    @Override
    public void onClick(View v) {

    }

    @Override
    public void onRefresh() {
        adapter.clear();
        loadUADataMore(pager,"hot",30);
        NetUtil.getData(ClassType.Banners,this);

    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        return false;
    }
}
